package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.dto.ExcelRectangularPillarModelDTO;
import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.TabRectangularPillarVO;
import com.powergisol.gis.vo.TabStairsVO;
import com.powergisol.gis.vo.TaskResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author : laishihao
 * @date : 2020/11/25
 * description :
 */
public class StairsExcelModelListener extends AnalysisEventListener {

    private final static Pattern NUMBER_PATTERN = Pattern.compile("([+\\-]?[0-9]+[.]?[\\d]*)");

    private TabStairsVO vo;

    //坐标系bean
    TabCoordinateSystem system;

    CacheManager cacheManager;

    //    String excelName;
    //字段值所在列数
    private Integer valueColumn;

    private ExcelRectangularPillarModelDTO rectangularPillarModelDTO;
    private BigDecimal lon;
    private BigDecimal lat;
    private BigDecimal altitude;


    public StairsExcelModelListener() {
    }


    public StairsExcelModelListener(TabStairsVO vo, TabCoordinateSystem system,
                                    CacheManager cacheManager, BigDecimal lon, BigDecimal lat, BigDecimal altitude,
                                    ExcelRectangularPillarModelDTO rectangularPillarModelDTO) {
        this.vo = vo;
        this.system = system;
        this.cacheManager = cacheManager;
        this.lon = lon;
        this.lat = lat;
        this.altitude = altitude;
        this.rectangularPillarModelDTO = rectangularPillarModelDTO;
    }


    @Override
    public void invoke(Object o, AnalysisContext context) {
        ArrayList data = (ArrayList) o;
        try {
            if (data.isEmpty()) {
                return;
            }
            int j = 0;
            int dataSize = data.size();
            for (int i = 0; i < dataSize; i++) {
                if (j == 1) {
                    return;
                }
                String str = String.valueOf(data.get(i));
                if (StringUtils.isBlank(str)) continue;

                //如果列数存在
                if (null == valueColumn && str.equals("字段值")) {
                    valueColumn = i;
                }

                switch (str) {
                    case "Hcdm01":
                        vo.setHcdm01(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm02":
                        vo.setHcdm02(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm03":
                        vo.setHcdm03(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm04":
                        vo.setHcdm04(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm05":
                        vo.setHcdm05(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm06":
                        vo.setHcdm06(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT21":
                        vo.setLT21(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "LT22":
                        vo.setLT22(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "LT23":
                        vo.setLT23(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "LT24":
                        vo.setLT24(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT25":
                        vo.setLT25(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT26":
                        vo.setLT26(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT27":
                        vo.setLT27(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT28":
                        vo.setLT28(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT29":
                        vo.setLT29(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT30":
                        vo.setLT30(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT31":
                        vo.setLT31(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "LT32":
                        vo.setLT32(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT33":
                        vo.setLT33(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT34":
                        vo.setLT34(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT36":
                        vo.setLT36(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT37":
                        vo.setLT37(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT38":
                        vo.setLT38(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT39":
                        vo.setLT39(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT40":
                        vo.setLT40(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT41":
                        vo.setLT41(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT42":
                        vo.setLT42(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT43":
                        vo.setLT43(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "LT44":
                        vo.setLT44(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
//            return;
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
    }

    private void saveData() {
        if (null == vo.getLT21() || !NUMBER_PATTERN.matcher(String.valueOf(vo.getLT21())).matches()) {
            dealFail("坐标Xl需为数字");
            return;
        }
        if (null == vo.getLT22() || !NUMBER_PATTERN.matcher(String.valueOf(vo.getLT22())).matches()) {
            dealFail("坐标Yw需为数字");
            return;
        }
        //存储原始值
        vo.setOLT21(vo.getLT21());
        vo.setOLT22(vo.getLT22());
        vo.setOLT23(vo.getLT23());

        BigDecimal LT21 = lon.multiply(new BigDecimal(1000)).add(vo.getLT21()).divide(new BigDecimal(1000));
        BigDecimal LT22 = lat.multiply(new BigDecimal(1000)).add(vo.getLT22()).divide(new BigDecimal(1000));
        BigDecimal LT23 = altitude.multiply(new BigDecimal(1000)).add(vo.getLT23()).divide(new BigDecimal(1000));

        BigDecimal tempKl21 = null;
        BigDecimal tempKl22 = null;
        //转换经纬度
        if (1 == vo.getIsChange()) {
            tempKl21 = XyChangeUtil.changex(LT21, LT22);
            tempKl22 = XyChangeUtil.changey(LT21, LT22);
        } else {
            lon = vo.getLT21();//经度
            lat = vo.getLT22();//纬度
        }

        if (tempKl21.compareTo(new BigDecimal("180")) > 0 || tempKl21.compareTo(new BigDecimal("-180")) < 0 ||
                tempKl22.compareTo(new BigDecimal("90")) > 0 || tempKl22.compareTo(new BigDecimal("-90")) < 0) {
            dealFail("坐标Xl或坐标Yw不在范围内");
            return;
        }
        //赋值计算过后的经纬度和高度
        vo.setLT21(tempKl21);
        vo.setLT22(tempKl22);
        vo.setLT23(LT23);

        rectangularPillarModelDTO.getStairsList().add(vo);

    }


    private void dealFail(String msg) {
        List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
        ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
        failedDataVo.setReason(msg);
        failedDataVo.setRowIndex(vo.getExcelName() + "中sheet " + vo.getSheetName());
        listFailed.add(failedDataVo);
        Cache cache = cacheManager.getCache("cache-data");
        TaskResultVo resultVo = new TaskResultVo();
        resultVo.setListFailed(listFailed);
        cache.put(vo.getSheetName() + vo.getUuid() + "sb", resultVo);
    }
}
